package com.lijinjiang.reflect04_method;

import org.junit.Test;

import java.lang.reflect.Method;

/**
 * @ClassName TestMethod01
 * @Description TODO
 * @Author Li
 * @Date 2022/9/20 21:41
 * @ModifyDate 2022/9/20 21:41
 * @Version 1.0
 */
public class TestMethod01 {
    // 1.getMethods()
    // 2.getDeclaredMethods()
    @Test
    public void testGetDeclaredMethods() {
        // 1.获取 class 对象
        Class c = Dog.class;
        // 2.获取成员方法数组
        Method[] methods = c.getDeclaredMethods();
        // 3.遍历
        for (Method method : methods) {
            System.out.println(method.getName() + " 返回值类型" + method.getReturnType() + " 参数个数" + method.getParameterCount());
        }
    }

    // 3.getMethod(String name, Class<?>... parameterTypes)
    // 4.getDeclaredMethod(String name, Class<?>... parameterTypes)
    @Test
    public void testGetDeclaredMethod() throws Exception {
        // 1.获取 class 对象
        Class c = Dog.class;
        // 2.获取成员方法数组
        Method method01 = c.getDeclaredMethod("eat");
        method01.setAccessible(true);
        Method method02 = c.getDeclaredMethod("eat",String.class);
        method02.setAccessible(true);
        // 3.执行方法
        Dog dog = new Dog();
        Object res01 = method01.invoke(dog);
        Object res02 = method02.invoke(dog, "稀饭");
        System.out.println(res01);
        System.out.println(res02);

    }
}
